import {Injectable} from '@angular/core';
import {NbComponentStatus, NbToastrService} from '@nebular/theme';
import {AlertController, LoadingController, Platform, ToastController} from '@ionic/angular';

@Injectable({
    providedIn: 'root',
})
export class NativeService {
    okButtonHandler = () => {
        console.log('okButtonClicked');
    }
    constructor(private toastrService: NbToastrService,
                private platform: Platform,
                private toastCtrl: ToastController,
                private alertCtrl: AlertController,
                private loadingCtrl: LoadingController) {
    }
    showToast(status: NbComponentStatus, body: string) {

        this.toastrService.show(body, '温馨提示', {status});
    }

    showAlert() {
    }

    showError(msg: string) {
        this.showToast('danger', msg);
    }

    showInfo(msg: string) {
        this.showToast('info', msg);
    }

    showSuccess(msg: string) {
        this.showToast('success', msg);
    }
    async showConfirm(message: string, okButtonText: string= '确定',
                      okButtonHandler= this.okButtonHandler, cancelButtonText: string= '取消',
                      cancelButtonHandler= this.okButtonHandler, header: string= '温馨提示') {
        await this.closeTips();
        const alert = await this.alertCtrl.create({
            header, message,
            buttons: [
                {text: cancelButtonText, handler: cancelButtonHandler},
                {text: okButtonText, handler: okButtonHandler},
            ],
        });
        return await alert.present();
    }
    private async closeTips() {
        // 第七步 先获取到是否有已经打开的弹框等
        const alert = await this.alertCtrl.getTop(); // 这个方法可以获取到当前正在显示的最顶层的弹框
        if (alert) {
            await alert.dismiss(); // 关闭
        }
        const toast = await this.toastCtrl.getTop();
        if (toast) {
            await toast.dismiss();
        }
        const loading = await this.loadingCtrl.getTop();
        if (loading) {
            await loading.dismiss();
        }
    }
}
